package com.zjuh.taofood.biz.dao.ibatis;

import com.zjuh.taofood.biz.dao.TaobaoItemDAO;
import com.zjuh.taofood.biz.domain.taobao.TaobaoItemDO;

public class TaobaoItemDAOIbatis extends BaseIbatisDAO implements TaobaoItemDAO {
	
	public long create(TaobaoItemDO taobaoItem) {
		return (Long)this.getSqlMapClientTemplate().insert("TaobaoItemDAO.create", taobaoItem);
	}

	public void delete(long id) {
		this.getSqlMapClientTemplate().update("TaobaoItemDAO.delete", id);
	}

	public void update(TaobaoItemDO taobaoItem) {
		this.getSqlMapClientTemplate().update("TaobaoItemDAO.update", taobaoItem);
	}

	public TaobaoItemDO queryById(long id) {
		return (TaobaoItemDO)this.getSqlMapClientTemplate().queryForObject("TaobaoItemDAO.queryById", id);
	}
	
	public TaobaoItemDO queryByItemId(long itemId) {
		return (TaobaoItemDO)this.getSqlMapClientTemplate().queryForObject("TaobaoItemDAO.queryByItemId", itemId);
	}

	@Override
	public void insertOrUpdateByItemId(TaobaoItemDO taobaoItemDO) {
		TaobaoItemDO itemInDb = queryByItemId(taobaoItemDO.getItemId());
		if (itemInDb != null) {
			// update
			taobaoItemDO.setId(itemInDb.getId());
			taobaoItemDO.setGmtCreate(itemInDb.getGmtCreate());
			update(taobaoItemDO);
		} else {
			// insert
			create(taobaoItemDO);
		}
	}

}
